package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtIncome;
import com.greyu.ysj.entity.CbtIncomeExample;
import com.greyu.ysj.entity.CbtTransfer;
import com.greyu.ysj.mapper.CbtIncomeMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtIncomeService;

@Service
@Transactional
public class CbtIncomeServiceImpl implements CbtIncomeService {
	@Autowired
    private CbtIncomeMapper cbtIncomeMapper;
	
	@Override
	public double selectTotalIncomeByuserId(int userId) {
		double totalIncome=cbtIncomeMapper.selectTotalIncomeByuserId(userId);
		return totalIncome;
	}

	@Override
	public double selectYesterdayIncomeByuserId(int userId) {
		double yesterdayIncome=cbtIncomeMapper.selectYesterdayIncomeByuserId(userId);
		return yesterdayIncome;
	}

	@Override
	public ResultModel save(CbtIncome cbtIncome) {
		if (null == cbtIncome.getUserId() 
				||  null == cbtIncome.getInvestId()
				||  null == cbtIncome.getDate()
				||  null == cbtIncome.getIncome()
        		) {
            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
        }
		//insert
        this.cbtIncomeMapper.insert(cbtIncome);
        return ResultModel.ok(cbtIncome);
	}

	@Override
	public CbtIncome getLeftIncomByInvestId(int investId) {
		CbtIncome cbtIncome=cbtIncomeMapper.getLeftIncomByInvestId(investId);
		System.out.println("cbtIncome&&&"+cbtIncome);
		return cbtIncome;
		
	}

	@Override
	public CbtIncome getIncomeByuserId(int userId) {
		CbtIncomeExample incomeExample = new CbtIncomeExample();
		CbtIncomeExample.Criteria criteria = incomeExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<CbtIncome> cbtIncomes = this.cbtIncomeMapper.selectByExample(incomeExample);

        CbtIncome cbtIncome;
        try {
        	cbtIncome = cbtIncomes.get(0);
        } catch (Exception e) {
            return null;
        }
        return cbtIncome;
	}

	@Override
	public double selectYesterdayIncomeByInvestId(int investId) {
		double yesterdayIncome=this.cbtIncomeMapper.selectYesterdayIncomeByInvestId(investId);
		return yesterdayIncome;
	}

	@Override
	public List<CbtIncome> selectTodayIncomeByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtIncome> list=this.cbtIncomeMapper.selectTodayIncomeByuserId(userId);
		return list;
	}
	
	
	
	@Override
	public int totalPages(Integer userId, Integer rows) {
		int total=countIncomeByuserId(userId);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}
	
	@Override
	public int countIncomeByuserId(int userId) {
		int count=this.cbtIncomeMapper.countIncomeByuserId(userId);
		return count;
	}

}
